import { defineStore } from 'pinia';
import pinia from '@/stores';
import { getRoutes } from '@/api/user/menu';
import type { IMenuState } from '@/types/user/menu';
import { handleMenuList } from '@/utils/router';
import router from '@/router';
export const useMenuStoreHook = defineStore('menu', {
    state: () => ({
        menuList: Array<IMenuState>()
    }),
    actions: {
        async storeGetRoutes() {
            return new Promise(async (resolve, reject) => {
                const { data: res } = await getRoutes();
                this.menuList = handleMenuList(res.data.slice(0, 4));
                // 添加动态路由
                this.menuList.forEach((route: IMenuState) => {
                    router.addRoute(route);
                });
                router.addRoute({
                    path: '/:pathMatch(.*)*',
                    redirect: '/not-found'
                });
                resolve(true);
            });
        }
    },
    persist: {
        key: 'menu',
        storage: localStorage,
        pick: ['menuList']
    }
});

export function useMenuStore() {
    return useMenuStoreHook(pinia);
}
